/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.interceptors.merchant;

import app.models.sys.AuthUsers;
import com.github.sog.controller.security.SecurityKit;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * .
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-05-25 11:36
 * @since JDK 1.6
 */
public class CommonInterceptor implements Interceptor {
    public static final String LOGIN_COMMON = "login_usr";

    @Override
    public void intercept(ActionInvocation ai) {
        final HttpServletRequest request = ai.getController().getRequest();
        if (!SecurityKit.isLogin(request)) {
            ai.getController().redirect("/merchant/login");
        } else {
            AuthUsers users = SecurityKit.getLoginUser(request);
            if (users != null) {
                ai.getController().setAttr(LOGIN_COMMON, users);
            }
            ai.invoke();
        }
    }
}
